﻿/***************************************************************************

 

Copyright (c) Microsoft Corporation. All rights reserved.

This code is licensed under the Visual Studio SDK license terms.

THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF

ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY

IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR

PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

 

***************************************************************************/


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace Tcvt.Packages
{
    partial class WorkItemForm : Form
    {
        private WorkItem m_wi;
        private FieldCollection m_fields;
        private int m_revision;

        public WorkItemForm(WorkItemStore store, string projectName, string typeName)
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            Project proj = store.Projects[projectName];
            WorkItemType type = proj.WorkItemTypes[typeName];

            m_wi = new WorkItem(type);
            m_fields = m_wi.Fields;
            m_revision = m_wi.Revision;
            SetRevisions();
        }

        public WorkItemForm(WorkItemStore store, int id)
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            
            m_wi = store.GetWorkItem(id);
            m_fields = m_wi.Fields;
            m_revision = m_wi.Revision;
            SetRevisions();
        }

        public WorkItemForm(WorkItemStore store, WorkItem wi)
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            
            m_wi = wi;
            m_fields = wi.Fields;
            m_revision = wi.Revision;
            SetRevisions();
        }


        private void WorkItemForm_Load(object sender, System.EventArgs e)
        {
            Text = String.Format(Text, m_wi.Id != 0 ? m_wi.Id.ToString() : "New");
            LoadFields();

        }

        private void listViewFields_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (listViewFields.FocusedItem != null)
            {
                ShowField();
                panelData.Visible = true;
            }
            else
            {
                panelData.Visible = false;
            }
        }

        private void ShowField()
        {
            Field fld = m_fields[listViewFields.FocusedItem.Text];

            editType.Text = fld.FieldDefinition.FieldType.ToString();
            comboValue.Enabled = fld.IsEditable;
            comboValue.Items.Clear();
            comboValue.Text = fld.Value != null ? fld.Value.ToString() : "";

            // Allowed values, required, valid are available only in the latest revision
            foreach (string item in fld.AllowedValues)
            {
                comboValue.Items.Add(item);
            }
            comboRequired.CheckState = fld.IsRequired ? CheckState.Checked : CheckState.Unchecked;
            comboValid.CheckState = fld.IsValid ? CheckState.Checked : CheckState.Unchecked;
            comboListItemsOnly.CheckState = fld.IsLimitedToAllowedValues ? CheckState.Checked : CheckState.Unchecked;
            comboDirty.CheckState = fld.IsDirty ? CheckState.Checked : CheckState.Unchecked;
            comboEditable.Checked = fld.IsEditable;

            editRefName.Text = fld.ReferenceName;
        }

        private void buttonSet_Click(object sender, System.EventArgs e)
        {
            Field fld = m_fields[listViewFields.FocusedItem.Text];
            object val = null;

            if (comboValue.Text != "")
            {
                val = comboValue.Text;
            }

            fld.Value = val;
            LoadFields();
        }

        private void buttonSave_Click(object sender, System.EventArgs e)
        {
            m_wi.Save();
            m_revision = m_wi.Revision;
            this.Text = String.Format("Work Item ({0})", m_wi.Id);
            LoadFields();
            SetRevisions();
        }

        private void LoadFields()
        {
            // Save the selection, if any
            string selected;

            if (listViewFields.SelectedItems.Count > 0)
            {
                selected = listViewFields.SelectedItems[0].Text;
            }
            else
            {
                selected = null;
            }

            listViewFields.Items.Clear();
            panelData.Visible = false;

            // Populate fields
            foreach (Field fld in m_fields)
            {
                ListViewItem lvi = new ListViewItem(fld.Name);
                string val = fld.Value == null ? "" : fld.Value.ToString();

                // Invalid: red
                // Read-only: gray
                // Required: yellow
                Color backColor = Color.FromKnownColor(KnownColor.Window);
                Color foreColor = Color.FromKnownColor(KnownColor.WindowText);

                if (!fld.IsEditable)
                {
                    foreColor = Color.FromKnownColor(KnownColor.GrayText);
                }

                // Required property is available only in the latest edition
                if (IsLatestRevision())
                {
                    if (fld.IsRequired)
                    {
                        backColor = Color.Yellow;
                    }
                }

                lvi.ForeColor = foreColor;
                lvi.BackColor = backColor;
                lvi.SubItems.Add(val != null ? val : "");
                val = fld.OriginalValue == null ? "" : fld.OriginalValue.ToString();
                lvi.SubItems.Add(val != null ? val : "");
                lvi.Tag = fld;
                listViewFields.Items.Add(lvi);
            }

            // Restore the selection
            if (selected != null)
            {
                ListViewItem item = listViewFields.FindItemWithText(selected);

                if (item != null)
                {
                    item.Focused = true;
                    item.Selected = true;
                    // Make it visible
                    listViewFields.EnsureVisible(item.Index);
                }
            }
        }

        private void SetRevisions()
        {
            labelRevision.Text = String.Format("Revision {0} of {1}", m_revision, m_wi.Revision);
            buttonRevNext.Enabled = m_revision < m_wi.Revision;
            buttonRevPrev.Enabled = m_revision > 0;
        }

        private void buttonRevPrev_Click(object sender, System.EventArgs e)
        {
            ChangeRevision(-1);
        }

        private void buttonRevNext_Click(object sender, System.EventArgs e)
        {
            ChangeRevision(+1);
        }

        private void ChangeRevision(int delta)
        {
            int newRev = m_revision + delta;

            m_fields = (newRev == m_wi.Revision ? m_wi.Fields : m_wi.Revisions[newRev].Fields);
            buttonSave.Enabled = buttonSet.Enabled = (newRev == m_wi.Revision);
            m_revision = newRev;

            SetRevisions();
            LoadFields();
        }

        private bool IsLatestRevision()
        {
            return m_revision == m_wi.Revision;
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}